package com.ssm.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.springboot.dao.TbPermissionDao;
import com.ssm.springboot.pojo.DataInfo;
import com.ssm.springboot.pojo.TbPermission;
import com.ssm.springboot.service.TbPermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * (TbPermission)表服务实现类
 *
 * @author makejava
 * @since 2021-12-27 21:50:50
 */
@Service("tbPermissionService")
public class TbPermissionServiceImpl implements TbPermissionService {
    @Resource
    private TbPermissionDao tbPermissionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbPermission queryById(Integer id) {
        return this.tbPermissionDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public DataInfo queryAllByLimit(int page,int limit) {
        PageHelper.startPage(page,limit);
        List<TbPermission> tbPermissions = tbPermissionDao.queryAllByLimit();
        PageInfo<TbPermission> of = PageInfo.of(tbPermissions);

        return new DataInfo(0,null,of.getTotal(),of.getList());
    }

    /**
     * 新增数据
     *
     * @param tbPermission 实例对象
     * @return 实例对象
     */
    @Override
    public DataInfo insert(TbPermission tbPermission) {
        List<TbPermission> permissionByUserid = tbPermissionDao.findPermissionByUserid(10);
        HashSet<String> objects = new HashSet<>();
        for(TbPermission tb:permissionByUserid){
            objects.add(tb.getPermissionName());
        }
        if(objects.contains(tbPermission.getPermissionName())){
            return new DataInfo(1,"已有此权限,请勿重复添加",null,null);
        }
        tbPermissionDao.insert(tbPermission);
        return new DataInfo(0,null,null,null);
    }

    /**
     * 修改数据
     *
     * @param tbPermission 实例对象
     * @return 实例对象
     */
    @Override
    public TbPermission update(TbPermission tbPermission) {
        this.tbPermissionDao.update(tbPermission);
        return this.queryById(tbPermission.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public DataInfo deleteById(Integer id) {

        int i = tbPermissionDao.deleteById(id);
        return new DataInfo(0,null,null,null);
    }
}